/*
 * BanBu_PurchaseRecord
 * @description 本文件提供表情购买记录界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供表情购买记录界面的ViewController
 */

#import "BanBu_PurchaseRecord.h"
#import "UIImage+emoImage.h"
#import "BanBu_SmileViewAPI.h"
#import "BanBu_PurchaseRecordCell.h"
#import "AppCommunicationManager.h"
#import "BanBu_EmoManger.h"
#import "BanBu_NavButton.h"
#import "BanBu_DetailEmoViewController.h"
#import "BanBu_ImageView.h"

@interface BanBu_PurchaseRecord ()

@end

@implementation BanBu_PurchaseRecord

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"buyingEmoRecord", nil);
    //获取下载记录
    [self getEmoInfo];
    [self createEditButton];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:img.image];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    [_emoRecordArr release],_emoRecordArr = nil;
    [super dealloc];
}

-(void)createEditButton
{
    //添加“删除”按钮
    BanBu_NavButton * actionButton = [[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal];
    actionButton.position = ButtonPositionRight;
    actionButton.frame=CGRectMake(0, 7,40, 30);
    actionButton.tag = 101;
    [actionButton addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    [actionButton setImage:[UIImage imageNamed:@"ic_topbar_more.png"] forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithCustomView:actionButton]autorelease];
}

-(void)edit
{
    UIActionSheet *aSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"cancelNotice", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"resetSale",nil), nil];
    aSheet.tag = 10101;
    [aSheet showInView:self.view.window];
    [aSheet release];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0://恢复购买记录
        {
            NSLog(@"case 0");
        }
            break;
        default:
            break;
    }
}

//获取已下载的
-(void)getEmoInfo
{
    NSDictionary * downloadEmoDic = [NSKeyedUnarchiver unarchiveObjectWithFile:DownloadInfoPath];
    
    NSMutableArray * array = [[NSMutableArray alloc] initWithArray:downloadEmoDic.allValues];
    
    [array sortUsingFunction:compare context:NULL];
    
    self.emoRecordArr = array;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _emoRecordArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 81;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EmoMangerCell";
    BanBu_PurchaseRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray * objects =[[NSBundle mainBundle]loadNibNamed:@"BanBu_PurchaseRecordCell" owner:self options:nil];
        
        for (id object in objects)
        {
            if ([object isMemberOfClass:[BanBu_PurchaseRecordCell class]])
            {
                cell = object;
                cell.backgroundColor = [UIColor clearColor];
                break;
            }
        }
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        [cell setBg];
    }
    
    NSDictionary * dic = [_emoRecordArr objectAtIndex:indexPath.row];
    
    [cell setEmoInfo:dic];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BanBu_DetailEmoViewController * detailEmoController = [[BanBu_DetailEmoViewController alloc]init];
    
    detailEmoController.emoInfoDic = [_emoRecordArr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detailEmoController animated:YES];
    [detailEmoController release];
}
@end
